package com.szqhyxj.youxiangju.biz.card;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.card.addCard.BindCardActivity;
import com.szqhyxj.youxiangju.biz.card.credit.CreditFragment;
import com.szqhyxj.youxiangju.biz.card.deposit.DepositFragment;
import com.szqhyxj.youxiangju.biz.merchantHome.MerchantContainerActivity;
import com.szqhyxj.youxiangju.biz.merchantHome.MerchantType;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * XiaoPanYu
 * on 2018/10/31
 */
public class CardActivity extends BaseActivity implements CardContarct.View, NavigationBar.NavigationBarListener{

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.credit)
    RadioButton credit;
    @BindView(R.id.deposit)
    RadioButton deposit;
    @BindView(R.id.credit_line)
    RadioButton creditLine;
    @BindView(R.id.deposit_line)
    RadioButton depositLine;
    @BindView(R.id.card)
    FrameLayout card;
    @BindView(R.id.card_line)
    RadioGroup cardLine;
    private CreditFragment creditFragment;
    private DepositFragment depositFragment;
    private CardPresenter cardApplyContarct;

    public static void start(Activity activity){
        Intent intent = new Intent(activity,CardActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card);
        ButterKnife.bind(this);
        FragmentManager fragmentManager = getSupportFragmentManager();
        final FragmentTransaction transaction = fragmentManager.beginTransaction();
        creditFragment = CreditFragment.newInstance();
        depositFragment = DepositFragment.newInstance();
        // 先添加再隐藏起来
        transaction.add(R.id.card, creditFragment);
        transaction.hide(creditFragment);
        transaction.add(R.id.card, depositFragment);
        transaction.hide(depositFragment);
        // 默认显示第一个
        transaction.show(creditFragment);
        // 提交事务
        transaction.commit();
        cardApplyContarct = new CardPresenter(this);
        initViews();
    }

    @Override
    protected void onResume(){
        super.onResume();
        //刷新信用卡数据
        creditFragment.showRefresh();
    }

    private void initViews(){
        navigationBar.setNavigationBarListener(this);
    }

    @Override
    public void showCredit(){
        cardLine.check(R.id.credit_line);
        getSupportFragmentManager().beginTransaction().hide(depositFragment).show(creditFragment).commit();
        navigationBar.setVisibilityRImg(View.VISIBLE);
    }

    @Override
    public void showDeposit(){
        cardLine.check(R.id.deposit_line);
        getSupportFragmentManager().beginTransaction().hide(creditFragment).show(depositFragment).commit();
        navigationBar.setVisibilityRImg(View.GONE);
    }

    @Override
    public void navAddCard(){
        BindCardActivity.start(this);
    }

    @OnClick({R.id.credit, R.id.deposit})
    public void onViewClicked(View view){
        switch(view.getId()){
            case R.id.credit:
                cardApplyContarct.credit();
                break;
            case R.id.deposit:
                cardApplyContarct.deposit();
                break;
        }
    }

    @Override
    public void navigationLeft(){
        MerchantContainerActivity.start(this,false,MerchantType.MY.getCode());
    }

    @Override
    public void navigationRight(){
        cardApplyContarct.addCard();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            MerchantContainerActivity.start(this,false,MerchantType.MY.getCode());
        }
        return super.onKeyDown(keyCode, event);
    }
}
